﻿function createFilterRegion(column, region) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="region_search" tabindex="-1" id="region_search"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))
    $.ajax({
        url: '/MSBO/ListRegion/',
        type: "POST",
        data: { data: 'branch' },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .region_search').select2({
                placeholder: "Select element",
                data: data_response
            });
            $('#' + column.table().header().parentNode.id + ' .region_search').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .region_search').val()).draw();
            });
        }
    });
}
function createFilterRegionFreedom(column, region) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="region_search" tabindex="-1" id="region_search"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))
    $.ajax({
        url: '/MSBO/ListRegionFreedom/',
        type: "POST",
        data: { data: 'branch' },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .region_search').select2({
                placeholder: "Select element",
                data: data_response
            });
            $('#' + column.table().header().parentNode.id + ' .region_search').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .region_search').val()).draw();
            });
        }
    });
}

function createFilterInputText(column) {
    var select = $('<input type="text" class="filtet-table sme_filter_remove" placeholder="Type here">')
                                                            .appendTo($(column.header()))
                                                            .on('keyup change', function () {
                                                                column.search(this.value).draw();
                                                            });
}
function createFilterStatus(column, id) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="" tabindex="-1" id="' + id + '"><option></option></select></fieldset>').appendTo($(column.header()))
    var data = [{ id: '2', text: 'All' }, { id: '0', text: 'Inactive' }, { id: '1', text: 'Active' }];
    if (id == 'cust_status_search') {
        data = [{ id: '2', text: 'All' }, { id: 'INACTIVE', text: 'Inactive' }, { id: 'ACTIVE', text: 'Active' }, { id: 'DORMANT', text: 'Dormant' }];
    }
    $('#' + id + '').select2({
        placeholder: "Select status",
        data: data
    });
    $('#' + id + '').on("change", function (e) {
        column.search($('#' + id + '').val()).draw();
    });
}
function createFilterCustType(column, data) {

    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="" tabindex="-1" id="cust_type_search"><option></option></select></fieldset>').appendTo($(column.header()))

    $('#cust_type_search').select2({
        placeholder: "Select status",
        data: data
    });
    $('#cust_type_search').on("change", function (e) {
        column.search($('#cust_type_search').val()).draw();
    });
}

function createFilterCustType(column, data, id) {

    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="" tabindex="-1" id="' + id + '"><option></option></select></fieldset>').appendTo($(column.header()))

    $('#' + id + '').select2({
        placeholder: "Select",
        data: data
    });
    $('#' + id + '').on("change", function (e) {
        column.search($('#' + id + '').val()).draw();
    });
}
function createFilterBranch(column) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="branch_search" tabindex="-1" id="branch_search"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))

    $.ajax({
        url: '/MSBO/ListBranch/',
        type: "POST",
        data: { data: 'branch' },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .branch_search').select2({
                placeholder: "Select element",
                data: data_response,
            });
            $('#' + column.table().header().parentNode.id + ' .branch_search').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .branch_search').val()).draw();
            });
        }
    });
}
function createFilterBranchFreedom(column) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="branch_search" tabindex="-1" id="branch_search"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))

    $.ajax({
        url: '/MSBO/ListBranchFreedom/',
        type: "POST",
        data: { data: 'branch' },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .branch_search').select2({
                placeholder: "Select element",
                data: data_response,
            });
            $('#' + column.table().header().parentNode.id + ' .branch_search').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .branch_search').val()).draw();
            });
        }
    });
}
function createFilterMSBOName(column, type) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="msbo_search' + type + '" tabindex="-1" id="msbo_search' + type + '"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))

    $.ajax({
        url: '/MSBO/ListMSBOName',
        type: "POST",
        data: { data: type },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .msbo_search' + type + '').select2({
                placeholder: "Select element",
                data: data_response,
            });
            $('#' + column.table().header().parentNode.id + ' .msbo_search' + type + '').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .msbo_search' + type + '').val()).draw();
            });
        }
    });
}

function createFilterMSBONameFreedom(column, type) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="msbo_search' + type + '" tabindex="-1" id="msbo_search' + type + '"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))

    $.ajax({
        url: '/MSBO/ListMSBONameFreedom',
        type: "POST",
        data: { data: type },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .msbo_search' + type + '').select2({
                placeholder: "Select element",
                data: data_response,
            });
            $('#' + column.table().header().parentNode.id + ' .msbo_search' + type + '').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .msbo_search' + type + '').val()).draw();
            });
        }
    });
}
function createFilterIndustry(column) {
    var select = $('<fieldset class="region_select_fieldset sme_filter_remove"> <select class="industry_search" tabindex="-1" id="industry_search"  multiple="multiple"><option></option></select></fieldset>').appendTo($(column.header()))

    $.ajax({
        url: '/CustomerManager/ListIndustry/',
        type: "POST",
        data: { data: 'industry' },
        success: function (data_response) {
            $('#' + column.table().header().parentNode.id + ' .industry_search').select2({
                placeholder: "Select element",
                data: data_response,
            });
            $('#' + column.table().header().parentNode.id + ' .industry_search').on("change", function (e) {
                column.search($('#' + column.table().header().parentNode.id + ' .industry_search').val()).draw();
            });
        }
    });
}

function createFilterBalance(column) {
    var select = $('<div style="display:inline-flex;display: -moz-groupbox;" class="sme_filter_remove" ><label for="from-value">Min</label><input type="number" id="from-value" class="filtet-table from-value" placeholder=" "><label for="to-value">Max</label><input type="number" id="to-value" class=" filtet-table to-value" placeholder=" "></div>').appendTo($(column.header()));

    $('#' + column.table().header().parentNode.id + ' .from-value').on('keyup', function () {
        var search_val = $('#' + column.table().header().parentNode.id + ' .from-value').val() + '-' + $('#' + column.table().header().parentNode.id + ' .to-value').val();
        column.search(search_val).draw();
    });

    $('#' + column.table().header().parentNode.id + ' .to-value').on('keyup', function () {
        var search_val = $('#' + column.table().header().parentNode.id + ' .from-value').val() + '-' + $('#' + column.table().header().parentNode.id + ' .to-value').val();
        column.search(search_val).draw();
    });

}

function createFilterBalanceMulti(column, count) {
    var select = $('<div style="display:inline-flex;display: -moz-groupbox;" class="sme_filter_remove" ><label for="from-value">Min</label><input type="number" id="from-value' + count + '" class="filtet-table from-value" placeholder=" "><label for="to-value">Max</label><input type="number" id="to-value' + count + '" class=" filtet-table to-value" placeholder=" "></div>').appendTo($(column.header()));

    $('#' + column.table().header().parentNode.id + ' #from-value' + count).on('keyup', function () {
        var search_val = $('#' + column.table().header().parentNode.id + ' #from-value' + count).val() + '-' + $('#' + column.table().header().parentNode.id + ' #to-value' + count).val();
        column.search(search_val).draw();
    });

    $('#' + column.table().header().parentNode.id + ' #to-value' + count).on('keyup', function () {
        var search_val = $('#' + column.table().header().parentNode.id + ' #from-value' + count).val() + '-' + $('#' + column.table().header().parentNode.id + ' #to-value' + count).val();
        column.search(search_val).draw();
    });
}


function createFilterRange(column, min, max, step) {
    var select = $(' <input class="search_column form-control sme_filter_remove" type="text" id="range_25" value="" name="range" />')
                                                                        .appendTo($(column.header()))
    if (step == null) {
        step = 0.1
    }
    $("#range_25").ionRangeSlider({
        type: "double",
        keyboard: true,
        step: step,
        min: min,
        max: max,
        onFinish: function (data) {
            dataSearch = data.from + '-' + data.to;
            column.search(dataSearch).draw();
        }
    });
}
function createFilterRangeMulti(column, min, max, step, count) {
    var select = $(' <input class="search_column form-control sme_filter_remove" type="text" id="range_' + count + '" value="" name="range" />')
                                                                        .appendTo($(column.header()))
    if (step == null) {
        step = 0.1
    }
    $('#range_' + count).ionRangeSlider({
        type: "double",
        keyboard: true,
        step: step,
        min: min,
        max: max,
        onFinish: function (data) {
            dataSearch = data.from + ';' + data.to;
            column.search(dataSearch).draw();
        }
    });
}

function createFilterDateMulti(column, minDate, maxDate, count) {
    var select = $('<input id="loanmatdt_search_datepicker' + count + '" type="text" class="filtet-table sme_filter_remove loanmatdt_search_datepicker">').appendTo($(column.header()))
    var cb = function (start, end, label) {
        $('#loanmatdt_search_datepicker' + count + ' span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    };

    var optionSet1 = {
        startDate: moment(),
        endDate: moment(),
        showDropdowns: true,
        showWeekNumbers: true,
        timePicker: false,
        timePickerIncrement: 1,
        timePicker12Hour: true,
        opens: 'left',
        buttonClasses: ['btn btn-default'],
        applyClass: 'btn-small btn-primary sme-pick-range-date',
        cancelClass: 'btn-small',
        format: 'DD/MM/YYYY',
        separator: ' - ',
        locale: {
            applyLabel: 'OK',
            cancelLabel: 'Clear',
            fromLabel: 'From',
            toLabel: 'To',
            customRangeLabel: 'Custom',
            daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
            monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            firstDay: 1
        }
    };
    if (minDate != null && maxDate != null) {
        optionSet1.startDate = minDate;
        optionSet1.endDate = maxDate;
        optionSet1.minDate = minDate;
        optionSet1.maxDate = maxDate;
    }

    $('#loanmatdt_search_datepicker' + count + ' span').html(moment().subtract(29, 'days').format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));

    $('#loanmatdt_search_datepicker' + count).daterangepicker(optionSet1, cb);

    //$('#' + column.table().header().parentNode.id + ' .loanmatdt_search_datepicker').attr("placeholder", "click here");

    $('#loanmatdt_search_datepicker' + count).on('apply.daterangepicker', function (ev, picker) {
        column.search(this.value).draw();
    });
}
function createFilterDate(column, minDate, maxDate) {
    var select = $('<input id="loanmatdt_search_datepicker" type="text" class="filtet-table sme_filter_remove loanmatdt_search_datepicker">').appendTo($(column.header()))
    var cb = function (start, end, label) {
        $('#' + column.table().header().parentNode.id + ' .loanmatdt_search_datepicker span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    };

    var optionSet1 = {
        startDate: moment(),
        endDate: moment(),
        showDropdowns: true,
        showWeekNumbers: true,
        timePicker: false,
        timePickerIncrement: 1,
        timePicker12Hour: true,
        opens: 'left',
        buttonClasses: ['btn btn-default'],
        applyClass: 'btn-small btn-primary sme-pick-range-date',
        cancelClass: 'btn-small',
        format: 'DD/MM/YYYY',
        separator: ' - ',
        locale: {
            applyLabel: 'OK',
            cancelLabel: 'Clear',
            fromLabel: 'From',
            toLabel: 'To',
            customRangeLabel: 'Custom',
            daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
            monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            firstDay: 1
        }
    };
    if (minDate != null && maxDate != null) {
        optionSet1.startDate = minDate;
        optionSet1.endDate = maxDate;
        optionSet1.minDate = minDate;
        optionSet1.maxDate = maxDate;
    }

    $('#' + column.table().header().parentNode.id + ' .loanmatdt_search_datepicker span').html(moment().subtract(29, 'days').format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));

    $('#' + column.table().header().parentNode.id + ' .loanmatdt_search_datepicker').daterangepicker(optionSet1, cb);

    //$('#' + column.table().header().parentNode.id + ' .loanmatdt_search_datepicker').attr("placeholder", "click here");

    $('#' + column.table().header().parentNode.id + ' .loanmatdt_search_datepicker').on('apply.daterangepicker', function (ev, picker) {
        column.search(this.value).draw();
    });
}